package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.adapter.PopListAdapter;
import com.glory.hzlionpda.base.Constant;
import com.glory.hzlionpda.bean.AreaBean;
import com.glory.hzlionpda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;

public class WelComeActivity extends FreeUI_BaseActivity {
    @BindView(R.id.tvArea)
    TextView tvArea;
    @BindView(R.id.ivArrow)
    ImageView ivArrow;
    @BindView(R.id.flArea)
    FrameLayout flArea;
    @BindView(R.id.tvConfirm)
    TextView tvConfirm;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog;
    private FreeUI_GeneralPop generalPop;
    private Animation animation, animationDismiss;

    private void requestArea() {
        NetUtils.requestGetNet("AREAGROUPREQUEST", this, new JSONObject(), new FreeUI_DialogEntityCallBack<AreaBean>(
                new TypeToken<AreaBean>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<AreaBean> response) {
                final List<String> list = response.body().getAREAGROUPLIST();
                if (list.size() > 0) {
                    llMain.setVisibility(View.VISIBLE);
                    tvArea.setText(list.get(0));
                    flArea.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (generalPop == null) {
                                View mView = LayoutInflater.from(WelComeActivity.this).inflate(R.layout.pop_choose_list, null);
                                RecyclerView recyclerView = mView.findViewById(R.id.rvList);
                                recyclerView.setLayoutManager(new LinearLayoutManager(WelComeActivity.this));
                                recyclerView.setItemAnimator(new DefaultItemAnimator());
                                recyclerView.addItemDecoration(new DividerItemDecoration(WelComeActivity.this, 1));
                                generalPop = new FreeUI_GeneralPop(WelComeActivity.this, mView
                                        , flArea.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                                final PopListAdapter popListAdapter = new PopListAdapter(list);
                                popListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                popListAdapter.isFirstOnly(true);
                                popListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        tvArea.setText(String.valueOf(adapter.getItem(position)));
                                        generalPop.dismiss();
                                    }
                                });
                                recyclerView.setAdapter(popListAdapter);
                                generalPop.setOutsideTouchable(true);
                                generalPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                    @Override
                                    public void onDismiss() {
                                        if (animationDismiss == null) {
                                            animationDismiss = new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF,
                                                    0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                                            animationDismiss.setDuration(500);
                                            animationDismiss.setFillAfter(true);
                                        }
                                        ivArrow.startAnimation(animationDismiss);
                                    }
                                });
                            }
                            if (generalPop.isShowing()) {
                                generalPop.dismiss();
                            } else {
                                if (animation == null) {
                                    animation = new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF,
                                            0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                                    animation.setDuration(500);
                                    animation.setFillAfter(true);
                                }
                                ivArrow.startAnimation(animation);
                                generalPop.showAsDropDown(flArea, 0, 0);
                            }
                        }
                    });
                    tvConfirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Constant.AREA = tvArea.getText().toString();
                            startActivity(new Intent(WelComeActivity.this, HomeActivity.class));
                            WelComeActivity.this.finish();
                        }
                    });
                } else {
                    requestFail();
                }
            }

            @Override
            public void onError(Response<AreaBean> response) {
                super.onError(response);
                loadError(response.getException(), "AREAGROUPREQUEST");
                requestFail();
            }
        });
    }

    private void requestFail() {
        freeUI_generalFragmentDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(
                getApplicationContext(), "重新获取", "直接进入", "获取区域信息失败，请重新获取。",
                new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        requestArea();
                        freeUI_generalFragmentDialog.dismiss();
                    }
                }, new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        freeUI_generalFragmentDialog.dismiss();
                        startActivity(new Intent(WelComeActivity.this, HomeActivity.class));
                        WelComeActivity.this.finish();
                    }
                });
        freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "freeUI_generalFragmentDialog");
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_choose_area;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        requestArea();
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }
}
